package cn.legend.travel.admin.destination.service.impl;

import cn.legend.travel.admin.destination.dao.cache.IDistrictCacheRepository;
import cn.legend.travel.admin.destination.dao.persist.repository.IDistrictRepository;
import cn.legend.travel.admin.destination.pojo.entity.District;
import cn.legend.travel.admin.destination.pojo.param.DistrictAddNewParam;
import cn.legend.travel.admin.destination.pojo.param.DistrictUpdateParam;
import cn.legend.travel.admin.destination.pojo.vo.DistrictAdminVO;
import cn.legend.travel.admin.destination.service.IDistrictService;
import cn.legend.travel.common.enumerator.ServiceCode;
import cn.legend.travel.common.exception.ServiceException;
import cn.legend.travel.common.pojo.po.DistrictSimplePO;
import cn.legend.travel.common.pojo.vo.PageData;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class DistrictServiceImpl implements IDistrictService {
    @Value("${travel.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    private final IDistrictRepository districtRepository;
    private final IDistrictCacheRepository districtCacheRepository;

    @Override
    public void rebuild() {
        districtCacheRepository.deleteAll();
        List<DistrictSimplePO> simplePOS = districtRepository.getByParent(0L);
        districtCacheRepository.saveDistrictList(0L, simplePOS);
        simplePOS.forEach(simplePO -> {
            Long id = simplePO.getId();
            List<DistrictSimplePO> list = districtRepository.getByParent(id);
            districtCacheRepository.saveDistrictList(id, list);
        });
    }

    @Override
    public void delete(Long id) {
        if (districtCacheRepository.listByParent(id) != null) {
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, "该目的地含有子集,不允许直接删除");
        }
        int row = districtRepository.delete(id);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_DELETE, "服务器忙,请稍后再试");
        }
    }

    @Override
    public PageData<DistrictAdminVO> listByParent(Long pId, Integer pageNum) {
        return districtRepository.listByParent(pId, pageNum, defaultQueryPageSize);
    }

    @Override
    public void update(DistrictUpdateParam param) {
        District district = new District();
        BeanUtils.copyProperties(param, district);
        int row = districtRepository.updateById(district);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_UPDATE, "服务器忙,请稍后再试");
        }
    }

    @Override
    public void addNew(DistrictAddNewParam param) {
        int row = districtRepository.addNew(param);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "服务器忙,请稍后再试");
        }
    }

    @Override
    public List<DistrictSimplePO> listAllByParent(Long parentId) {
        return districtCacheRepository.listByParent(parentId);
    }
}
